<template>
	<!-- 进行考试-->
	<view class="body">
		<view>
			<Answer :isShowTime='true' :time='data.time' :isReviewed='false' currentSwiperItem='0' :duration='300'
				:questtionList='questtionList' @submit='handleSubmit' @onChange='onChange'></Answer>
		</view>
	</view>
</template>

<script>
	import myHeader from '../components/head.vue'
	import NetWorkTool from '@/api/netWorkTool'
	import Answer from './../components/dyw-answer/answer.vue'
	export default {
		data() {
			return {
				title: '考试中',
				questtionList: [],
				loadingMessage: '',
				confirmSubmitContent: '',
				errMessage: '',
				answers: [],
				time: 40,
				data: {},

			}
		},
		onLoad: function(option) {
			this.data = JSON.parse(option.data)
		},
		created() {
			// 初始化当前用户id
			this.init();
		},
		components: {
			Answer
		},
		methods: {
			// 初始化题目
			init() {
				NetWorkTool.POSTFORM('/api/weekly-question/questionIdQuery', {
					questionId: this.data.questionId
				}, true).then(res => {
					let arr = [];
					for (let item of res) {
						let o = {};
						o.id = item.id
						o.title = item.subject
						o.answer = item.answer
						o.score = item.score
						o.type = this.testEnum(item.type)
						let explainContent = item.userAnswer == undefined ? "" : item.userAnswer
						o.explainContent = "您的答案：" + explainContent
						o.userAnswer = ""
						o.difficulty = ""
						// 解析选项
						let opts = [];
						let paserObj = JSON.parse(item.options)
						let mp = new Map(Object.entries(paserObj));
						mp.forEach((value, key, mp) => {
							opts.push({
								optionId: key,
								id: key,
								content: value,
								type: this.testEnum(item.type),
								questionId: item.id
							})
						})
						o.optionList = opts
						arr.push(o)
					}
					this.questtionList = arr
				})
			},
			/* 提交答案
			 */
			handleSubmit(questtionList) {
				let arr = this.answers;
				let totalScore = 0;
				// 排除重复项
				for (var i = 0; i < arr.length; i++) {
					if (arr[i] == arr[i + 1]) {
						arr.splice(i, 1)
					}
				}
				// 算分
				for (let item of arr) {
					let map = new Map();
					map.set(item.questionId,item.userAnswer)
					if (item.question_answer == item.userAnswer) {
						totalScore += item.score
					}
				}
				let param = {
					"userScore": totalScore,
					"weeklyId": this.data.id,
					"options": arr
				}
				console.log(param)
				for(var i = 0; i < 3; i++) {
				    setTimeout(function () {
				        uni.hideLoading();
				    	uni.showToast({
				    		title: '正在提交试卷，请稍等···',
				    		icon: 'none',
				    	});
				    }, 1000);
				}
				
				NetWorkTool.POST('/api/weekly-question/finishExam',param,true).then(res=>{
					uni.showToast({
						title: '提交成功！',
						during: 2000,
					});
					uni.redirectTo({
						url: './index',
					})
				})
				
			},
			/* 题目答案变化
			 */
			onChange(answer) {
				this.answers.push({
					questionId: answer.id,
					userAnswer: answer.userAnswer,
					question_answer: answer.answer,
					score: answer.score
				})
			},
			testEnum(desc) {
				if (desc == '单选题') {
					return 2;
				}
				if (desc == '多选题') {
					return 3;
				}
			}
		}
	}
</script>

<style lang="scss">
	@import url("/static/party_building/assets/style/federation.css");

	page {
		background-color: #FFFFFF;
	}
</style>
